package edu.whu.powersoap;

import java.util.ArrayList;

import com.baidu.mapapi.cloud.CloudPoiInfo;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class RouteProvider extends ContentProvider{

	public static final String TAG = "RouteProvider";
	
	public static final String AUTHORITY = "powersoap.route";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/");
	
	public static final String VOLUMN_NUMBER = "number";
	public static final String VOLUMN_STATUS = "status";
	public static final String VOLUMN_TIME = "time";
	public static final String VOLUNM_EXECUTOR = "executor";
    public static final String VOLUMN_ROU_LIST = "rouList";
    
	private static final String DATABASE_NAME = "route.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "route";
	
	private DatabaseHelper mOpenHelper;
	
//	private ArrayList<CloudPoiInfo> mRotList = new ArrayList<CloudPoiInfo>();

	
	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
		
		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		
		Log.i(TAG, "getType---uri=" + uri);
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		long rowId = db.insert(TABLE_NAME, null, values);
		if(rowId > 0){
			uri = ContentUris.withAppendedId(uri, rowId);
			sendNotify(uri);
			return uri;
		}
		
		throw new SQLException("Failed to insert row into: " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = db.delete(TABLE_NAME, selection, selectionArgs);
		if(count > 0){
			sendNotify(uri);
		}
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = db.update(TABLE_NAME, values, selection, selectionArgs);
		if(count > 0){
			sendNotify(uri);
		
		}
		return count;
	}
	
	public void sendNotify(Uri uri){
		getContext().getContentResolver().notifyChange(uri, null);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE IF NOT EXISTS " 
					+ TABLE_NAME 
					+ "(number TEXT, status TEXT, time TEXT, executor TEXT, x DOUBLE, y DOUBLE)");
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrade database from " + oldVersion + " to " + newVersion);
			
		}
		
	}

}
